package fr.bordeaux1.planingdesetudiants.view;

import java.sql.SQLException;
import java.util.List;

import org.eclipse.swt.SWT;
import org.eclipse.swt.dnd.DND;
import org.eclipse.swt.dnd.DragSource;
import org.eclipse.swt.dnd.DragSourceEvent;
import org.eclipse.swt.dnd.DragSourceListener;
import org.eclipse.swt.dnd.TextTransfer;
import org.eclipse.swt.dnd.Transfer;
import org.eclipse.swt.events.PaintEvent;
import org.eclipse.swt.events.PaintListener;
import org.eclipse.swt.graphics.Color;
import org.eclipse.swt.graphics.Font;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.graphics.RGB;
import org.eclipse.swt.widgets.Canvas;
import org.eclipse.swt.widgets.Composite;

import fr.bordeaux1.planingdesetudiants.model.Cours;
import fr.bordeaux1.planingdesetudiants.model.Groupe;
import fr.bordeaux1.planingdesetudiants.model.Ue;

public class CoursCanvas extends Canvas {

	private Cours cours;
	private IFenetrePrincipale fenetre;
	private Ue ue;
	private List<Groupe> grps;

	public CoursCanvas(Composite parent, Cours cours, IFenetrePrincipale fenetre) {
		super(parent, SWT.NONE);
		this.cours = cours;
		this.fenetre = fenetre;

		addPaintListener(new PaintListener() {
			public void paintControl(PaintEvent arg0) {
				paint(arg0);
			}
		});

		DragSource source = new DragSource(this, DND.DROP_MOVE);
		Transfer[] types = new Transfer[] { TextTransfer.getInstance() };
		source.setTransfer(types);
		source.addDragListener(new DragSourceListener() {
			@Override
			public void dragStart(DragSourceEvent arg0) {
				System.out.println("dragStart");

			}

			@Override
			public void dragSetData(DragSourceEvent arg0) {
				System.out.println("dragSetData");
				if (TextTransfer.getInstance().isSupportedType(arg0.dataType))
					arg0.data = "" + CoursCanvas.this.cours.getId();
			}

			@Override
			public void dragFinished(DragSourceEvent arg0) {
				System.out.println("dragFinish");
				CoursCanvas.this.fenetre.modificationApportees();
			}
		});
	}

	public void paint(PaintEvent evt) {
		evt.gc.setTextAntialias(SWT.ON);
		if (ue == null) {
			try {
				ue = fenetre.getDb().chargerUe(cours.getCodeUe());
			} catch (SQLException e) {
				e.printStackTrace();
				return;
			}
		}
		
		if(cours.getType().equals(Cours.TYPE_COURS_MAGISTRAL)){
			setBackground(new Color(evt.display, new RGB(168, 255, 255)));
		}else if(cours.getType().equals(Cours.TYPE_TD)){
			setBackground(new Color(evt.display, new RGB(168, 255, 168)));
		}else if(cours.getType().equals(Cours.TYPE_TP)){
			setBackground(new Color(evt.display, new RGB(190, 168, 211)));
		}else {
			setBackground(new Color(evt.display, new RGB(192, 192, 192)));
		}
		
		String code = ue.getCode()+" ("+cours.getType()+")";
		Point size = setMaxTextSize(evt,code );
		evt.gc.drawText(code, 3/* (getSize().x - size.x) / 2 */,
				(int) (getSize().y * 0.2 - size.y / 2));

		size = setMaxTextSize(evt, ue.getLibelle());
		evt.gc.drawText(ue.getLibelle(), 3/* (getSize().x - size.x) / 2 */,
				(int) (getSize().y * 0.4 - size.y / 2));
		
		size = setMaxTextSize(evt, cours.getSalle());
		evt.gc.drawText(cours.getSalle(), 3/* (getSize().x - size.x) / 2 */,
				(int) (getSize().y * 0.6 - size.y / 2));

		if (grps == null) {
			try {
				grps = fenetre.getDb().listerGroupesDeCours(cours);
			} catch (SQLException e) {
				e.printStackTrace();
				return;
			}
		}
		String text = "";
		for (Groupe groupe : grps) {
			text += groupe.getNom() + " ";
		}
		size = setMaxTextSize(evt, text);
		evt.gc.drawText(text, 3/* (getSize().x - size.x) / 2 */,
				(int) (getSize().y * 0.8 - size.y / 2));

	}

	public Point setMaxTextSize(PaintEvent evt, String text) {
		int fontsize = 10;
		Point size;
		do {
			evt.gc.setFont(new Font(fenetre.getShell().getDisplay(), "Verdana",
					fontsize--, SWT.NORMAL));
			size = evt.gc.textExtent(text);
		} while (size.x > getSize().x && fontsize > 6);

		return size;
	}
	

}
